package org.mokusakura.web.listener;

import org.mokusakura.persistence.mapper.VisitHistoriesMapper;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.sql.Timestamp;

/**
 * @author MokuSakura
 */
@WebListener
public class VisitListener implements HttpSessionListener {
    private VisitHistoriesMapper visitHistoriesMapper;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        if (visitHistoriesMapper == null) {
            visitHistoriesMapper =
                    WebApplicationContextUtils.getRequiredWebApplicationContext(
                            se.getSession().getServletContext())
                            .getBean(VisitHistoriesMapper.class);
        }
        visitHistoriesMapper.insertVisitHistories(new Timestamp(System.currentTimeMillis()));

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {

    }
}
